<template>
  <a-modal
    title="新建部别"
    :width="500"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    okText="确认"
    cancelText="取消"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="部别名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['unitName', {rules: [{required: true, message: '请输入部别名称', trigger: 'blur'}]}]"></a-input>
        </a-form-item>
        <a-form-item
          label="部别类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            v-decorator="['unitType', {rules: [{required: true, message: '请选择部别类型'}]}]"
            placeholder="请选择部别类型"
            @change="handleChange"
          >
            <!--<a-select-option :value="1">旅</a-select-option>-->
            <a-select-option :value="2">营/部</a-select-option>
            <a-select-option :value="3">连/科</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="上级部别"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <div v-if="this.isShow">
            <a-select
              disabled
              v-decorator="['unitFather', {rules: [{required: true, message: '请选择上级部别'}]}]"
            >
              <a-select-option v-for="unit in unitFathers" :key="unit.unitId">{{ unit.unitName }}</a-select-option>
            </a-select>
          </div>
          <div v-else>
            <a-select
              placeholder="请选择上级部别"
              v-decorator="['unitFather', {rules: [{required: true, message: '请选择上级部别'}]}]"
            >
              <a-select-option v-for="unit in fatherList" :key="unit.unitId">{{ unit.unitName }}</a-select-option>
            </a-select>
          </div>
        </a-form-item>
        <a-form-item
          label="备注信息"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['unitInfo']"></a-input>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { addUnit } from '@/api/basic/unit'

export default {
  props: {
    unitFathers: {
      type: Array,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      isShow: false,
      fatherList: [],

      form: this.$form.createForm(this)
    }
  },
  methods: {
    add () {
      this.fatherList = []
      this.visible = true
      for (const index in this.unitFathers) {
        if (this.unitFathers[index].unitType === 2) {
          this.fatherList.push(this.unitFathers[index])
        }
      }
    },
    handleChange (value) {
      if (value === 2) {
        this.isShow = true
        this.form.setFieldsValue({
          'unitFather': 1
        })
      } else {
        this.isShow = false
        this.form.setFieldsValue({
          'unitFather': null
        })
      }
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          addUnit(values)
            .then(res => {
              if (res.message !== 'error') {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                this.$emit('ok')
              } else {
                this.confirmLoading = false
                this.$message.error('操作失败！')
              }
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
